<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<base href="daohangzhe"/>
<meta http-equiv="Content-Type" content="text/html; charset=Utf-8" />
<title>采购入库单</title>
<link rel="stylesheet" type="text/css"
	href="../extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="../extjs/ext-all.js"></script>
<script type="text/javascript" src="../extjs/ext-lang-zh_CN.js"></script>
<script>
Ext.define('Form2', {
	extend: 'Ext.data.Model',
}); 
var yeshu=1;
var zongyeshu=0;
var temp;//主从表数据
var data; //数据   
var rec = Ext.create('Form2');//创建mode  
//禁用
function jinyong(){
	
	 formpalcd.items.each(function(f) {
	        f.setReadOnly(true); 
	   });
	   formsg.items.each(function(f) {
		    f.setReadOnly(true);
     });
	   grids.disable();	  
}
//开启
function kaiqi(){
	  formpalcd.items.each(function(f) {
	        f.setReadOnly(false); 
	    });
	    formsg.items.each(function(f) {
	        f.setReadOnly(false); 
	    });
	    grids.enable();
	
}
//获取主从表数据
function huoqu(){
	var x1=grids.getStore();
	var time=Ext.Date.format(Ext.getCmp('billDate').getValue(), 'Y-m-d');
	 temp={
			   custID:Ext.getCmp('custID').getValue(),
		 	   billDate:time,
			   billNO:Ext.getCmp('billNO').getValue(),
			   classID:Ext.getCmp('classID').getValue(),
			   currID:Ext.getCmp('currID').getValue(),
			   priceofTax:Ext.getCmp('priceofTax').getValue(),
			   exchRate:Ext.getCmp('exchRate').getValue(),
			   wareid:Ext.getCmp('wareid').getValue(),
			   hasForeignTrade:Ext.getCmp('hasForeignTrade').getValue(),
			   pK_pingzheng:Ext.getCmp('pK_pingzheng').getValue(),
			   salesID:Ext.getCmp('salesID').getValue(),
			   maker:Ext.getCmp('maker').getValue(),
			   departName:Ext.getCmp('departName').getValue(),
			   permitter:Ext.getCmp('permitter').getValue(),
			   projectName:Ext.getCmp('projectName').getValue(),
			   zhuangtai:Ext.getCmp('zhuangtai').getValue(),
			  zongjine:Ext.getCmp('zongjine').getValue(),
			   list1:Ext.pluck(x1.data.items,"data"),
		};
	
}

//设置单号
function danhao(time){
	if(time==null || time==""){
	 time=Ext.Date.format(new Date(), 'Y-m-d');	 
	 Ext.getCmp('billDate').setValue(time);
	}
	   Ext.Ajax.request({
			  url: '/cgrk/danhao?date='+time+'', 
			  async:false,
			    success: function(response){ 
			    	Ext.getCmp('billNO').setValue(response.responseText);
			    	
			    } 
		 });
}


//计算金钱
function jisui(){
	var zongjine=0.00;
	Ext.each(Ext.pluck(grids.getStore().data.items,'data'), function(obj,i) {  
		var number=0;
		var jine=0.00;
		var price=0.00;
		var shuie=0.00;
		var shuilv;
		var hsjine=0.00; 
		shuilv=obj.shuilv;
		shuilv=parseFloat(shuilv)/100;
		number=obj.shuliang;
		price=obj.price;
		jine=(number*price);
		
		shuie=(parseInt(jine*shuilv));
		hsjine=(jine+shuie);
		grids.store.getAt(i).set('jine',jine)  ;
		grids.store.getAt(i).set('shuie',shuie)  ;
		grids.store.getAt(i).set('hanshuiprice',hsjine)  ;
		zongjine+=hsjine;
    });
	Ext.getCmp('zongjine').setValue(zongjine);
      
	 
}

//页面初始分页加载
function x(){
	 Ext.Ajax.request({
		  url: '/cgrk/list?page='+yeshu+'', 
		  async:false,
		  success: function(response){ 
		    	 data=response.responseText;
		    	 var obj = JSON.parse(data);
		    	 zongyeshu=obj.maxpage;
		    	 var data1= obj.pagedata; 
		    	 grids.getStore().removeAll();
		   		 grids.getStore().add(obj.pagedata.list1);
			     rec.data = Ext.apply(rec.data,data1);
			 }	
	});  
	 formpalcd.loadRecord(rec);
	 formsg.loadRecord(rec);
	 jinyong();


}
// 订单转入代码
function ddzr(bino){
	
	 var time=Ext.Date.format(Ext.getCmp('billDate').getValue(), 'Y-m-d');
		Ext.Ajax.request({
			url: '/cgrk/find1', 
			params :{no:bino},
			  async:false,
			    success: function(response){ 
			    	 data=response.responseText;
			    	 var obj = JSON.parse(data);
			     	 grids.getStore().removeAll();
					 grids.getStore().add(obj.list1);	
					 rec.data = Ext.apply(rec.data,obj);
					 formpalcd.loadRecord(rec);
					 formsg.loadRecord(rec);
					
					 danhao(); 
					 jisui();
			  } ,
			  failure : function(response) { 
			    	alert("转单失败"+response.status); 
			  } 
		});
	
}

	Ext.onReady(function(){
	    x();
		mainWindow.show();		
    });


var fielmenu=new Ext.menu.Menu({
		items:[{text:'历史交易查询',},{text:'分摊费用明细'}]
	});
var fielmenu2=new Ext.menu.Menu({
	items:[{text:'采购订单转入',listeners:{  
		 'click':function(){ 
			 windows2.show();
			 }
	}},{text:'采购询价转入',listeners:{  
			 'click':function(){ 
				 
				
			 }}
		 
		 }]
});
	var fielmenu3=new Ext.menu.Menu({
		items:[{text:'新增',listeners:{
			'click':function(){ 
				 formpalcd.getForm().reset();
				 formsg.getForm().reset();
			     
			    grids.getStore().removeAll();
			    danhao();  
			    kaiqi(); 
			 
			}  
		}},{text:'保存',listeners:{  
			 'click':function(){ 
				 huoqu();
		//	alert(JSON.stringify(temp));
			Ext.Ajax.request({
				  url: '/cgrk/insert', 
				  jsonData:JSON.stringify(temp),
				  async:false,
				    success: function(response){ 
				    	alert("保存成功");
				    	yeshu==1;
				    	x();
				    }, 
				    failure : function(response) { 
				    	alert("保存失败"+response.status); 
				    	} 
			 
			 });
		 
		} 
		}},{text:'下一单',listeners:{  
			 'click':function(){  
				 if(yeshu==1){
					 alert("已经是第一单了")
				 }else{
				 yeshu--;
				 x();}
				 
			 }}	
		},{text:'上一单',listeners:{  
			 'click':function(){  
				 if(yeshu==zongyeshu){
					alert("已经是最后单了") 
				 }else{
				 yeshu++;
				 x();
				 }
				 
			 }}	
		}]
	});
	

	var grids=Ext.create('Ext.grid.Panel',{
		  listeners: {  
			  'containerdblclick':function(grid,rowIndex,columnIndex,e ){
				
				  grids.getStore().add({});
			  }, 'edit':function(){
				 
				  jisui();
			    }
			  },plugins: [
	        				Ext.create('Ext.grid.plugin.CellEditing', {
	            				clicksToEdit: 1
	            	
	        				})
	    				] ,
		columns: [
					 {header: '（栏号）',xtype: 'rownumberer',dataIndex: 'line',  width: 50,sortable: false},
					 { header: '物料编号', dataIndex: 'prodId',width: 120,editor: { xtype:'textfield'} },
	       			 { header: '物料名称', dataIndex: 'prodName',width: 120,editor: { xtype:'textfield'} },
						 { header: '单位名称', dataIndex: 'danwei',editor: { xtype:'textfield'}  },
						 { header: '数量', dataIndex: 'shuliang',editor: { xtype:'textfield'} },
						 { header: '折扣前单价', dataIndex: 'zhikoudanjia',editor: { xtype:'textfield'} },
						 { header: '折数(%)', dataIndex: 'zheshu',editor: { xtype:'textfield'} },
						 { header: '单价', dataIndex: 'price',editor: { xtype:'textfield'}  },
						 { header: '金额', dataIndex: 'jine' ,editor: { xtype:'textfield'} },
						 { header: '税率', dataIndex: 'shuilv',editor: { xtype:'textfield'}  },
						 { header: '税额', dataIndex: 'shuie' ,editor: { xtype:'textfield'} },
						 { header: '含税金额', dataIndex: 'hanshuiprice',editor: { xtype:'textfield'}  },
						 { header: '赠品', dataIndex: 'isgift',editor: { xtype:'textfield'}  },
						 { header: '分录备注', dataIndex: 'remake',editor: { xtype:'textfield'}  },
						 { header: '来源单别', dataIndex: 'danbie', editor: { xtype:'textfield'}},
						 { header: '来源单号', dataIndex: 'billNo',editor: { xtype:'textfield'}  },
						 { header: '分摊费用', dataIndex: 'fentang',editor: { xtype:'textfield'}  }
	   					 ],
	   			 height: 130,
				 width:1971,
				 autoScroll:false,
				 
	})
	
	
	var ppc=Ext.create('Ext.form.Panel', {
		 
	    bodyPadding: 5,
		height:'100%',
		layout:"column",
		baseCls:'x-plain',
		items:[{
			 xtype: 'textfield',
	        name: 'SalesMan',
			bodyPadding: 5,
	        fieldLabel: '账款归属',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
			 xtype: 'datefield',
	        name: 'Marker',
	        fieldLabel: '付款日期',
			style:'margin-left:19px',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
			 xtype: 'textfield',
	        name: 'ProdDepart',
	        fieldLabel: '付款条件',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
			 xtype: 'datefield',
	        name: 'Permitter',
	        fieldLabel: '账款月份',
			style:'margin-left:19px',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				}],
		});
	var ppa = Ext.create('Ext.panel.Panel', {
	    bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
	    width: 1985,
		height:170,
	    items:[grids],
		bbar:[{xtype:"label",text:'总计:',width:90},
		{xtype:"textfield",disabled:true,width:90,margin:'0 180',value:'0.00',id:'zongjine'}]
	});

	
	var  tbl=Ext.create('Ext.TabPanel',{
		width:"100%",
		height:230,
		autoScroll:true,
		bodyPadding: 5,
		items:[{title:'内容',autoScroll:true,items:[ppa]},{title:'账款',autoScroll:true,items:[ppc]},{title:'备注',xtype:'textarea',name:'Remark',fieldLabel: '备注',labelWidth: 30,}]
	});

	
	var formsg=Ext.create('Ext.form.Panel', {
	    bodyPadding: 5,
		height:'100%',
		layout:"column",
		baseCls:'x-plain',
		 defaults: {
	    //	 readOnly:true,
	         anchor: '100%'
	    },
		items:[{
			id:'salesID',
			 xtype: 'textfield',
	        name: 'salesID',
			bodyPadding: 5,
	        fieldLabel: '采购人员',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
					id:'maker',
			 xtype: 'textfield',
	        name: 'maker',
	        fieldLabel: '制单人员',
			style:'margin-left:19px',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
					id:'departName',
			 xtype: 'textfield',
	        name: 'departName',
	        fieldLabel: '所属部门',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
					id:'permitter',
			 xtype: 'textfield',
	        name: 'permitter',
	        fieldLabel: '复核人员',
			style:'margin-left:19px',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				},{
					id:'projectName',
			 xtype: 'textfield',
	        name: 'projectName',
	        fieldLabel: '所属项目',
			width:250,
	        labelWidth: 80,
	        anchor: '100%',
				}],
		});

	

var formpalcd=Ext.create('Ext.form.Panel', { //表单
	
	bodyPadding: 5,
    width:'100%',
    layout: 'column',
    defaults: {
   //	 readOnly:true,
   
        anchor: '100%'
   },
    defaultType: 'textfield',
    items: [{
    	id:"custID",
    	    style:"color:blue;text-align:center",
			xtype: 'textfield',
       		name: 'custID',
			fieldLabel: '供应商',
			 allowBlank: false,
       		labelWidth: 70,
       		hidden: true, 
       		hideLabel:true 
			},{
		    	id:"gysname",
	    	    style:"color:blue;text-align:center",
				xtype: 'textfield',
	       		name: 'gysname',
				fieldLabel: '供应商',
				 allowBlank: false,
	       		labelWidth: 70,
	       		
				},{
		id:'billDate',
		style:'margin-left:9px;color:blue;text-align:center',
		 xtype: 'datefield',
        name: 'billDate',
        fieldLabel: '单据日期',
        labelWidth: 100,
        anchor: '100%',
			},{
				id:'gysaddress',
		style:"text-align:center",
		 xtype: 'textfield',
        name:'gysaddress',
        fieldLabel: '供应商地址',
        labelWidth: 70,
        anchor: '100%',
			},{
				id:'billNO',
			style:'margin-left:9px;color:blue;text-align:center',
		 xtype: 'textfield',
        name: 'billNO',
        fieldLabel: '单据号码',
        labelWidth: 100,
	
        anchor: '100%',
			},{
				id:'classID',
		style:'text-align:center',
		 xtype: 'textfield',
        name: 'classID',
        fieldLabel: '入库类型',
        labelWidth: 70,
        anchor: '100%',
			},{
				id:'currID',
			style:'margin-left:9px;text-align:center',
		 xtype: 'textfield', //多行文本域
        name: 'currID',
        fieldLabel: '币别',
        labelWidth: 100,
        anchor: '100%',
			},{
				id:'priceofTax',
				style:'text-align:center',		
		name:'priceofTax',
	    xtype:'combo', 
	    value:'含税',
        fieldLabel: '单价含税',
        store : new Ext.data.ArrayStore({
			fields : ['value', 'text'],
			data : [["1", '含税'], ["2", '未含税']]
		}),   
        labelWidth: 70,
        anchor: '100%',
			},{
				id:'exchRate',
			style:'margin-left:9px;text-align:center',
		 xtype: 'textfield',  //数值
        name: 'exchRate',
        fieldLabel: '汇率',
        labelWidth: 100,
        anchor: '100%'
        },{
        	id:'wareid',
        	  name: 'wareid',
   		 xtype: 'combo',
   		 value:"北京仓",
   		 style:'color:blue;text-align:center',
         fieldLabel: '仓库',
         store : new Ext.data.ArrayStore({
 			fields : ['value', 'text'],
 			data : [["1", '北京仓'], ["2", '上海仓']]
 		}),   
         allowBlank: false,
         labelWidth: 70,
         anchor: '100%',
 			},{
 				id:'hasForeignTrade',
 			style:'margin-left:9px;text-align:center',
 		 xtype: 'textfield',  //数值
         name: 'hasForeignTrade',
         fieldLabel: '国外贸易',
         labelWidth: 100,
         anchor: '100%'
         },{
        		id:'pK_pingzheng',
        	 name: 'pK_pingzheng',
        	 style:'text-align:center',
       		 xtype: 'textfield',
             fieldLabel: '凭证编号',
             labelWidth: 70,
             anchor: '100%',
     			},{
     				id:'zhuangtai',
     	 			style:'margin-left:9px;text-align:center',
     	 		 xtype: 'textfield',  //数值
     	         name: 'zhuangtai',
     	        value:"未审核",
     	         fieldLabel: '状态',
     	         labelWidth: 100,
     	         anchor: '100%',
     	         }]
     
});
//订单查询
var grids2=Ext.create('Ext.grid.Panel',{ 
	id:'grids2',
	name:'grids2',
	listeners:{
		'itemdblclick' : function(grid, rowIndex, e,i){
			var bino=grid.store.getAt(i).get('billno');
			ddzr(bino);
			windows2.hide();
		}  		
    },
	columns: [
		{header: '（栏号）',
	     xtype: 'rownumberer',
	     dataIndex: 'grids2', 
	     width: 58,
		 sortable: false
        },
        { header: '采购订单号',  dataIndex: 'billno', width: 96},
        { header: '负责人',  dataIndex: 'maker', width: 96}
        ],
   	
   	width:349,
   	height: 240,
	autoScroll:true,	
})
//订单查询
var windows2=new  Ext.Window({
	width:360,
	height:300,
	title:"订单查询",
	closable:true,
	resizable:false, //设置是否可以改变大小
	draggable:true,
	closeAction: 'hide',
	anchor: '100%',
	items:[grids2],
	listeners: {
		activate : function() {	
			var time=Ext.Date.format(Ext.getCmp('billDate').getValue(), 'Y-m-d');
			Ext.Ajax.request({
				url:"/cgrk/findorder",
				params :{time:time},
				method : "post",
				async: false,
				success : function(data){
					var de=Ext.JSON.decode(data.responseText);
					grids2.getStore().loadData(de);
				}
			});
    	}
	 } 
});



var mainWindow = new Ext.Window({ 
		width:640,
		height:600,
		title:"采购入库",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true, //设
		items:[formpalcd,tbl,formsg],
		bbar:[{xtype:"splitbutton",text:'查询',width:90,menu:fielmenu},{xtype:"splitbutton",text:'转单',width:90,menu:fielmenu2},{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu3}
		,{xtype:"splitbutton",text:'审核',listeners:{  
			 'click':function(){  
				huoqu();
				 Ext.Ajax.request({
					  url: '/cgrk/update1', 
					  jsonData:JSON.stringify(temp),
					  async:false,
					    success: function(response){ 
					    	alert("审核成功");
					    	jisui();
					    	x();
					  } 
				 });
			
			 }}	},{xtype:"splitbutton",text:'反审核',listeners:{  
				 'click':function(){  
					 var billno=Ext.getCmp('billNO').getValue();
					 Ext.Ajax.request({
						  url: '/cgrk/update2?id='+billno+'', 
						  async:false,
						    success: function(response){ 
						    	alert("反审核成功");
						    	x();
						  } 
					 });
				
				 }}	},{xtype:"splitbutton",text:'删除',listeners:{  
					 'click':function(){  
						 var billno=Ext.getCmp('billNO').getValue();
						 Ext.Ajax.request({
							  url: '/cgrk/update3?id='+billno+'', 
							  async:false,
							    success: function(response){ 
							    	alert("删除成功");
							    	yeshu--;
							    	x();
							  } 
						 });
					
					 }}	},{xtype:"splitbutton",text:'修改',listeners:{
						 'click':function(){
							 kaiqi();
						 }
						
					 }}],
	});

function fromaction(){
	alert(Ext.encode(formpalcd.getForm().getValues()));// 序列化表单
}

</script>
</head>

<body>
</body>
</html>
